<?
/* 
  $Id: cms.php,v 1.1 2008/03/05 15:48:34 tiger Exp $ 
 
  Open Source E-Commerce Solutions 
  http://www.oscommerce.com 
 
  Copyright (c) 2002 osCommerce.com 
 
  Released under the GNU General Public License 
*/

define('NO_CMS_IMAGE', 'noimage.gif');

function blockVars($sTpl,$beginSymbol='',$endSymbol='')
  {
    if(strlen($beginSymbol) == 0) $beginSymbol = "{";
    if(strlen($endSymbol) == 0) $endSymbol = "}";
    $beginSymbolLength = strlen($beginSymbol);
    $endTag = 0;
    /* searching open symbol */
    while (($beginTag = strpos($sTpl,$beginSymbol,$endTag)) !== false)
    {
      /* searching close symbol */
      if (($endTag = strpos($sTpl,$endSymbol,$beginTag)) !== false)
      {
        /* add variable name to the array of variable */
        $vars[] = substr($sTpl, $beginTag + $beginSymbolLength, $endTag - $beginTag - $beginSymbolLength);
      }
    }
    /* if there is one or more variable in array then return this array */
    if(isset($vars)) return $vars;
    else return false;
  }

function tep_cms_image_replace($content_text)
{
	$result = $content_text;
	$vars = blockVars($result);
	if($vars) foreach($vars as $key => $image_name){
		$content_image_query = tep_db_query("select content_images_image from " . TABLE_CONTENT_IMAGES . " where content_images_name='" . $image_name . "'");
		if(tep_db_num_rows($content_image_query)) {
			$content_image = tep_db_fetch_array($content_image_query);
			$result = str_replace("{".$image_name."}", tep_image(DIR_WS_CMS_IMAGES . $content_image['content_images_image']), $result);
		}else{
			$result = str_replace("{".$image_name."}", tep_image(DIR_WS_CMS_IMAGES . NO_CMS_IMAGE), $result);
		}
	}

	return $result;
}


function tep_get_cms_content($content_name)
{
	global $languages_id;

	// 20080305 itx freddy modified the where to verify content_status = '1'
	$content_query = tep_db_query("select content_text as text from " . TABLE_CONTENT . " c, "  . TABLE_CONTENT_DESCRIPTION .  " cd where c.content_status = '1' and c.content_id=cd.content_id and content_name='" . $content_name . "' and cd.language_id='" . (int)$languages_id . "'");

	if(!tep_db_num_rows($content_query)) return '';

	$content = tep_db_fetch_array($content_query);
	$result = nl2br($content['text']);
	$result = tep_cms_image_replace($result);

	return $result;
}

// 20080305 itx freddy new function to get the title
function tep_get_cms_title($content_name)
{
	global $languages_id;

	$content_query = tep_db_query("select content_title as title from " . TABLE_CONTENT . " c, "  . TABLE_CONTENT_DESCRIPTION .  " cd where c.content_status = '1' and c.content_id=cd.content_id and content_name='" . $content_name . "' and cd.language_id='" . (int)$languages_id . "'");

	if(!tep_db_num_rows($content_query)) return '';

	$content = tep_db_fetch_array($content_query);

	return $content['title'];
}
?>